package com.example.roomdatabasetest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.roomdatabasetest.recyclerView.CarRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //Ui assets
    private EditText editBrand;
    private EditText editPrice;
    private Button btnAdd;
    private Button btnQueryAll;
    private RecyclerView recyclerView;
    //RecyclerView Assets
    CarRecyclerViewAdapter carRecyclerViewAdapter;
    List<Car> cars = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        setFunction();
        initRecyclerView();
    }

    private void initRecyclerView() {
        cars = CarMode.getInstance(getApplicationContext()).queryAll();
        carRecyclerViewAdapter = new CarRecyclerViewAdapter(cars);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setAdapter(carRecyclerViewAdapter);
        recyclerView.setLayoutManager(manager);
    }

    private void setFunction() {
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Car car = new Car();
                car.setBrand(editBrand.getText().toString());
                car.setPrice(Integer.parseInt(editPrice.getText().toString()));
                //添加到数据库中
                CarMode.getInstance(getApplicationContext()).addItem(car);
                //carRecyclerViewAdapter.notifyItemInserted(carRecyclerViewAdapter.getItemCount());
                //因为Recycler View的更新是要直接对其数据进行更新，只是更换一个是没有用的。所以自己测试一下用那种方法是最好的
                /*
                1. 先查再通过Recyclerview进行操作，这个是有效的，但是个人感觉效率并不高。
                cars = CarMode.getInstance(getApplicationContext()).queryAll();
                car = cars.get(cars.size()-1);
                cars = carRecyclerViewAdapter.getCars();
                cars.add(car);
                2. 同一个cars列表对象，再对该对象进行操作，但是这个就有一个问题就是没有获取数据库中的ID——在界面中展示的item中的id就默认0；不好这个方法
                List<Car> carsList = carRecyclerViewAdapter.getCars();
                carsList.add(car);
                 */
                cars = CarMode.getInstance(getApplicationContext()).queryAll();
                car = cars.get(cars.size()-1);
                cars = carRecyclerViewAdapter.getCars();
                cars.add(car);
                carRecyclerViewAdapter.notifyItemInserted(cars.size());
                //editText上的数据清空且光标定位到brand上
                editBrand.setText("");
                editPrice.setText("");
                editBrand.setSelection(0);//光标定位
            }

        });
        btnQueryAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cars = CarMode.getInstance(getApplicationContext()).queryAll();
                carRecyclerViewAdapter = new CarRecyclerViewAdapter(cars);
                carRecyclerViewAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initUI() {
        editBrand = findViewById(R.id.edit_brand);
        editPrice = findViewById(R.id.edit_price);
        btnAdd = findViewById(R.id.btn_add);
        btnQueryAll = findViewById(R.id.btn_queryAll);
        recyclerView = findViewById(R.id.recyclerView);
    }
}